package com.emm.yixun.website.service.pmo;

import com.emm.yixun.common.model.PmoPush;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.PmoPushDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.IsPushEnum;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.system.service.PmoPushServiceFacade;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("pmoPushService")
public class PmoPushServiceImpl implements PmoPushService {
	
	private final Logger logger = LoggerFactory.getLogger(PmoPushServiceImpl.class);

	@Autowired
	private PmoPushServiceFacade pmoPushServiceFacade;
	@Autowired
	private LevelProjectDataService levelProjectDataService;

	/**
	 * 流程结束后添加待推送记录
	 * @param projectId
	 * @param projectPmoCode
	 * @param pushMonth
	 * @param processInstanceId
	 * @param businessName
	 */
	public void addPmoPushData(Long projectId, String projectPmoCode, String pushMonth, String processInstanceId, String businessName){
		User user = ShiroUtil.getCurrentUser();
		if(null != user){
			//校验是否有推送PMO数据的必要
			boolean flag = levelProjectDataService.sendValid(user.getMerchantId());
			if(flag){
				PmoPushDto pmoPushDto = new PmoPushDto();
				pmoPushDto.setProjectId(projectId);
				pmoPushDto.setPmoCode(projectPmoCode);
				pmoPushDto.setPushMonth(pushMonth);
				pmoPushDto.setIsPushed(IsPushEnum.TOPUSH.getNo());
				ResponseList<PmoPush> pmoPushResList = pmoPushServiceFacade.findByDto(pmoPushDto);
				if(pmoPushResList.isSuccess()){
					List<PmoPush> pmoPushList = pmoPushResList.getEntity();
					if(null == pmoPushList || pmoPushList.size() == 0){
						pmoPushDto.setProcInstId(processInstanceId);
						pmoPushDto.setBusinessName(businessName);
						pmoPushDto.setCreateUserId(user.getId());
						pmoPushDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
						pmoPushServiceFacade.save(pmoPushDto);
					}
				}
			}
		}

	}
}
